/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef SYSTEM_ATOMIC_H_
#define SYSTEM_ATOMIC_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cstdint>
#include "node/node.h"
#include "node/system/mutex.h"
#include "node/system/lockguard.h"

namespace node {
    namespace system {

        template<typename T>
        class AtomicValue
        {
        public:
            AtomicValue(void) : lock_(), value_(0) {}
            AtomicValue(T val) : lock_(), value_(val) {}
            ~AtomicValue(void){}

            T operator++(void)
            {
                NODE_GUARD_LOCK_ASSERT_RETURN_VAR(lock_, value_);
                return ++value_;
            }
            T operator++(int)
            {
                NODE_GUARD_LOCK_ASSERT_RETURN_VAR(lock_, value_);
                T old_value = value_;
                ++value_;
                return old_value;
            }

            T operator--(void)
            {
                NODE_GUARD_LOCK_ASSERT_RETURN_VAR(lock_, value_);
                return --value_;
            }
            T operator--(int)
            {
                NODE_GUARD_LOCK_ASSERT_RETURN_VAR(lock_, value_);
                T old_value = value_;
                --value_;
                return old_value;
            }

            T operator+=(T val)
            {
                NODE_GUARD_LOCK_ASSERT_RETURN_VAR(lock_, value_);
                value_ += val;
                return value_;
            }
            T operator-=(T val)
            {
                NODE_GUARD_LOCK_ASSERT_RETURN_VAR(lock_, value_);
                value_ -= val;
                return value_;
            }

            operator T(void) const
            {
                return value_;
            }

        private:
            ::node::system::Mutex lock_;
            volatile T value_;
            DISALLOW_COPY_AND_ASSIGN(AtomicValue);
        };

        typedef AtomicValue<::std::int8_t>   AtomicInt8;
        typedef AtomicValue<::std::uint8_t>   AtomicUint8;
        typedef AtomicValue<::std::int16_t>   AtomicInt16;
        typedef AtomicValue<::std::uint16_t>   AtomicUint16;
        typedef AtomicValue<::std::int32_t>   AtomicInt32;
        typedef AtomicValue<::std::uint32_t>   AtomicUint32;
        typedef AtomicValue<::std::int64_t>   AtomicInt64;
        typedef AtomicValue<::std::uint64_t>   AtomicUint64;
    }
}

#endif // SYSTEM_ATOMIC_H_
